import {parseTime, request} from "../../utils/util";

const app = getApp()
Page({
  data: {
    top: app.globalData.rightBarInfo.top,
    height: app.globalData.rightBarInfo.height,
    winHeight: app.globalData.systemInfo.screenHeight,
    type: [
      {
        id: 0,
        name: '全部'
      },
      {
        id: 1,
        name: '待支付'
      },
      {
        id: 2,
        name: '待使用'
      },
      {
        id: 3,
        name: '使用中'
      },
      {
        id: 4,
        name: '已使用'
      },
      {
        id: 7,
        name: '已退款'
      }
    ],
    typeId: 0, // 当前订单类型
    currentIndex: 0, // 列表索引
    allData: {
      0: {
        list: [],
        page: 1,
        loadMore: true,
      },
      1: {
        list: [],
        page: 1,
        loadMore: true,
      },
      2: {
        list: [],
        page: 1,
        loadMore: true,
      },
      3: {
        list: [],
        page: 1,
        loadMore: true,
      },
      4: {
        list: [],
        page: 1,
        loadMore: true,
      },
      7: {
        list: [],
        page: 1,
        loadMore: true,
      },
    },
  },
  selectOrder (e) {
    this.setData({
      typeId: e.currentTarget.dataset.id,
      currentIndex: e.currentTarget.dataset.index
    })
  },
  swiperChange (e) {
    this.setData({
      typeId: this.data.type[e.detail.current].id
    })
    this.getList()
  },
  // 获取列表数据
  getList(page) {
    wx.showLoading({
      title: '加载中..',
      mask: true
    })
    request({
      url: '/api/web/venue/orders',
      params: {
        page,
        status: this.data.typeId,
        fold: 0
      },
      success: ({ code, data, message }) => {
        wx.hideLoading()
        if (code === 200) {
          let currentData = data.data.map(item => {
            item.created_at_show = parseTime(item.created_at, '{y}/{m}/{d} {h}:{i}')
            item.order_sub.forEach(item1 => {
              item1.order_date_show = item1.order_date.substr(5)
              item1.start_time_show = item1.start_time.substr(0, 5)
              item1.end_time_show = item1.end_time.substr(0, 5)
            })

            if (item.status === 1) {
              item.statusClass = 'order-head-right'
            } else if (item.status === 2) {
              item.statusClass = 'order-head-right-b'
            } else if (item.status === 3 || item.status === 4 || item.status === 6) {
              item.statusClass = 'order-head-right-blue'
            }
            return item
          })
          if (data.current_page === 1) {
            this.data.allData[this.data.typeId].list = []
          }
          this.setData({
            allData: {
              ...this.data.allData,
              [this.data.typeId]: {
                list: [
                  ...this.data.allData[this.data.typeId].list,
                  ...currentData
                ],
                page: data.current_page,
                loadMore: data.current_page !== data.last_page
              }
            },
          })
        } else {
          wx.showToast({
            title: message,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },
  handleSuccess() {
    this.getList()
  },
  scrollToLower () {
    if (!this.data.allData[this.data.typeId].loadMore) {
      return false
    }
    this.getList(this.data.allData[this.data.typeId].page + 1)
  },
  onShow () {
    wx.setNavigationBarColor({
      frontColor: '#000000',
      backgroundColor: '#000000',
      animation: {
        duration: 400,
        timingFunc: 'easeIn'
      }
    })
    this.setData({
      top: app.globalData.rightBarInfo.top,
      height: app.globalData.rightBarInfo.height,
      winHeight: app.globalData.systemInfo.screenHeight
    })
    this.getList()
  },
})
